import { login, getInfo, queryAuthorityList } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import MD5 from 'js-md5'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    userInfo: {},
    authorityList: [] // 所有权限列表
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: state => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USERINFO: (state, data) => {
    state.userInfo = data
  },
  SET_AUTHORITY: (state, data) => {
    state.authorityList = data
  }
}

const actions = {
  // 用户登录
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', 'asdsadsadsadasdasd')
      setToken('asdsadsadsadasdasd')
      resolve()
      // login({ loginName: username.trim(), password: MD5(password) })
      //   .then(data => {
      //     const { token } = data
      //     commit('SET_TOKEN', token)
      //     setToken(token)
      //     resolve()
      //   })
      //   .catch(error => {
      //     reject(error)
      //     resolve()
      //   })
    })
  },

  // 获取用户信息
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
      getInfo()
        .then(data => {
          if (!data) {
            return reject('验证失败，请重新登录')
          }
          const { userName, headImgUrl } = data
          commit('SET_NAME', userName)
          commit('SET_AVATAR', headImgUrl)
          commit('SET_USERINFO', data)

          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // 获取所有权限树
  getAuthorityList({ commit }) {
    return new Promise((resolve, reject) => {
      queryAuthorityList()
        .then(data => {
          commit(
            'SET_AUTHORITY',
            JSON.parse(
              JSON.stringify(data.children)
                .replace(/authId/g, 'key')
                .replace(/authName/g, 'title')
            )
          )

          resolve(data)
        })
        .catch(error => {
          reject(error)
        })
    })
  },

  // 退出登录
  logout({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      resetRouter()
      commit('RESET_STATE')
      resolve()

      // logout(state.token)
      //   .then(() => {
      //     removeToken() // must remove  token  first
      //     resetRouter()
      //     commit('RESET_STATE')
      //     resolve()
      //   })
      //   .catch(error => {
      //     reject(error)
      //   })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
